package flaud

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CaseManagementApi struct{}

// CreateCaseManagement 创建案件管理
// @Tags CaseManagement
// @Summary 创建案件管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body flaud.CaseManagement true "创建案件管理"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /CM/createCaseManagement [post]
func (CMApi *CaseManagementApi) CreateCaseManagement(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var CM flaud.CaseManagement
	err := c.ShouldBindJSON(&CM)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = CMService.CreateCaseManagement(ctx, &CM)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteCaseManagement 删除案件管理
// @Tags CaseManagement
// @Summary 删除案件管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body flaud.CaseManagement true "删除案件管理"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /CM/deleteCaseManagement [delete]
func (CMApi *CaseManagementApi) DeleteCaseManagement(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	err := CMService.DeleteCaseManagement(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteCaseManagementByIds 批量删除案件管理
// @Tags CaseManagement
// @Summary 批量删除案件管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /CM/deleteCaseManagementByIds [delete]
func (CMApi *CaseManagementApi) DeleteCaseManagementByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := CMService.DeleteCaseManagementByIds(ctx, IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateCaseManagement 更新案件管理
// @Tags CaseManagement
// @Summary 更新案件管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body flaud.CaseManagement true "更新案件管理"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /CM/updateCaseManagement [put]
func (CMApi *CaseManagementApi) UpdateCaseManagement(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var CM flaud.CaseManagement
	err := c.ShouldBindJSON(&CM)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = CMService.UpdateCaseManagement(ctx, CM)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindCaseManagement 用id查询案件管理
// @Tags CaseManagement
// @Summary 用id查询案件管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询案件管理"
// @Success 200 {object} response.Response{data=flaud.CaseManagement,msg=string} "查询成功"
// @Router /CM/findCaseManagement [get]
func (CMApi *CaseManagementApi) FindCaseManagement(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	reCM, err := CMService.GetCaseManagement(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(reCM, c)
}

// GetCaseManagementList 分页获取案件管理列表
// @Tags CaseManagement
// @Summary 分页获取案件管理列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query flaudReq.CaseManagementSearch true "分页获取案件管理列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /CM/getCaseManagementList [get]
func (CMApi *CaseManagementApi) GetCaseManagementList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo flaudReq.CaseManagementSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := CMService.GetCaseManagementInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetCaseManagementPublic 不需要鉴权的案件管理接口
// @Tags CaseManagement
// @Summary 不需要鉴权的案件管理接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /CM/getCaseManagementPublic [get]
func (CMApi *CaseManagementApi) GetCaseManagementPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	CMService.GetCaseManagementPublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的案件管理接口信息",
	}, "获取成功", c)
}
